package com.bjsxt.ego.goods.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.ego.common.service.impl.BaseServiceImpl;
import com.bjsxt.ego.common.util.ConstatFinalUtil;
import com.bjsxt.ego.common.util.PageInfoUtil;
import com.bjsxt.ego.goods.dao.IGoodsBrandDao;
import com.bjsxt.ego.goods.dao.IGoodsCategoryDao;
import com.bjsxt.ego.goods.dao.IGoodsDao;
import com.bjsxt.ego.goods.pojo.Goods;
import com.bjsxt.ego.goods.pojo.GoodsBrand;
import com.bjsxt.ego.goods.pojo.GoodsCategory;
import com.bjsxt.ego.goods.service.IGoodsService;
/**
 * 商品service实现类
 * @author 章鸿敏
 *
 */
@Service("goodsService")
public class GoodsServiceImpl extends BaseServiceImpl implements IGoodsService
{
	@Resource
	private IGoodsBrandDao goodsBrandDao;
	@Resource
	private IGoodsCategoryDao goodsCategoryDao;
	@Resource
	private IGoodsDao goodsDao;
	
	/**********************************商品品牌模块开始******************************/
	/**
	 *  保存一条商品品牌
	 * @param goodsBrand
	 * @return
	 */
	@Override
	public JSONObject saveOneGoodsBrandService(GoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.save(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	/**
	 *  保存一条商品品牌
	 * @param goodsBrand
	 * @param paranmMap
	 * @return
	 */
	@Override
	public JSONObject saveOneGoodsBrandService(GoodsBrand goodsBrand, Map<String, Object> paramsMap)
	{
		/*保存品牌*/
		JSONObject resultJSON = this.saveOneGoodsBrandService(goodsBrand);
		if ("0".equalsIgnoreCase(resultJSON.getString("code")))
		{
			/*保存品牌成功*/
			/*
			 * 上传图片:
			路径
			网络路径:http://127.0.0.1:8080/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			真实路径:d:/project/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			相对路径:/uploadfile/brand/2017-02-13/uuid.jpg
			 * */
			/* 原始文件的名称 oriFile  "null" */
			String oriFileName = paramsMap.get("oriFileName") + "";
			InputStream inputStream = (InputStream) paramsMap.get("inputStream");
			
			if (paramsMap.get("oriFileName")!= null && !"".equalsIgnoreCase(oriFileName))
			{
				/*相对路径前一部分*/
				String relaPath = ConstatFinalUtil.BUNDLE.getString("file.relaPath");
				/*开始拼装相对路径*/
				relaPath +="/brand/" + this.dateFormatUtil.formatDate(new Date()) + "/" 
				+UUID.randomUUID().toString().replaceAll("-", "");
				
				/*真实路径*/
				String truePath = ConstatFinalUtil.BUNDLE.getString("file.truePath") +relaPath;
				File tarFile = new File(truePath);
				
				try
				{
					if (!tarFile.getParentFile().exists())
					{
						/*父目录不存在,就创建*/
						tarFile.getParentFile().mkdirs();
					}
					FileOutputStream fos = new FileOutputStream(tarFile);
					/*文件拷贝*/
					if (!this.fileUtil.copyFile(inputStream, fos))
					{
						ConstatFinalUtil.LOGGER.error("拷贝文件失败了,{}",tarFile);
						/*拷贝文件失败,就抛异常*/
						String str = null;
						str.toString();
					}else
					{
						goodsBrand.setLogoPath(relaPath);
						this.updateOneGoodsBrandService(goodsBrand);
					}
						
				} catch (FileNotFoundException e)
				{
				}
			}
		}
		return resultJSON;
	}
	

	/**
	 * 更新一条商品品牌
	 */
	@Override
	public JSONObject updateOneGoodsBrandService(GoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.update(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	/**
	 * 更新一条商品品牌
	 */
	@Override
	public JSONObject updateOneGoodsBrandService(GoodsBrand goodsBrand, Map<String, Object> paramsMap)
	{
		/*更新品牌*/
		JSONObject resultJSON = this.updateOneGoodsBrandService(goodsBrand);
		if ("0".equalsIgnoreCase(resultJSON.getString("code")))
		{
			/*更新品牌成功*/
			/*
			 * 上传图片:
			路径
			网络路径:http://127.0.0.1:8080/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			真实路径:d:/project/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			相对路径:/uploadfile/brand/2017-02-13/uuid.jpg
			 * */
			/* 原始文件的名称 oriFile  "null" */
			String oriFileName = paramsMap.get("oriFileName") + "";
			InputStream inputStream = (InputStream) paramsMap.get("inputStream");
			
			if (paramsMap.get("oriFileName")!= null && !"".equalsIgnoreCase(oriFileName))
			{
				/*数据库存储的是:老的LOGO的相对路径*/
				String oldLogoPath = goodsBrand.getLogoPath();
				File oldFile = new File(ConstatFinalUtil.BUNDLE.getString("file.truePath") +oldLogoPath); 
				
				/*相对路径前一部分*/
				String relaPath = ConstatFinalUtil.BUNDLE.getString("file.relaPath");
				/*开始拼装相对路径*/
				relaPath +="/brand/" + this.dateFormatUtil.formatDate(new Date()) + "/" 
				+UUID.randomUUID().toString().replaceAll("-", "");
				
				/*真实路径*/
				String truePath = ConstatFinalUtil.BUNDLE.getString("file.truePath") +relaPath;
				File tarFile = new File(truePath);
				
				try
				{
					if (!tarFile.getParentFile().exists())
					{
						/*父目录不存在,就创建*/
						tarFile.getParentFile().mkdirs();
					}
					FileOutputStream fos = new FileOutputStream(tarFile);
					/*文件拷贝*/
					if (!this.fileUtil.copyFile(inputStream, fos))
					{
						ConstatFinalUtil.LOGGER.error("拷贝文件失败了,{}",tarFile);
						/*拷贝文件失败,就抛异常*/
						String str = null;
						str.toString();
					}else
					{
						goodsBrand.setLogoPath(relaPath);
						JSONObject resDBJSON = this.updateOneGoodsBrandService(goodsBrand);
						if ("0".equalsIgnoreCase(resDBJSON.getString("code")))
						{
							/*表示上传图片成功，现需要删除老的图片*/
							if (oldFile.exists())
							{
								if (!oldFile.delete())
								{
									ConstatFinalUtil.LOGGER.error("删除品牌老LOGO失败了,路径：{}",oldFile);
								}
							}
						}
					}
				} catch (FileNotFoundException e)
				{
				}
			}
		}
		return resultJSON;
	}


	/**
	 * 删除一条商品品牌记录
	 */
	@Override
	public JSONObject deleteOneGoodsBrandService(GoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.delete(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public GoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}

	@Override
	public List<GoodsBrand> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%，实现模糊匹配*/
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页*/
			/* 查询总的记录数*/
			condMap.put("pageMarking", "true");
			List<GoodsBrand> goodsBrandList =this.goodsBrandDao.findCondList(condMap);
			/* 取得总记录数*/
			if (goodsBrandList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(goodsBrandList.get(0).getId());
			}
			
			/*开始分页,查询数据*/
			condMap.put("pageMarking", "false");
			/*limit ?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.goodsBrandDao.findCondList(condMap);
		}
		/*不分页查询数据*/
		return this.goodsBrandDao.findCondList(condMap);
	}

	/**********************************商品品牌模块结束******************************/
	
	/**********************************商品分类模块开始******************************/
	/**
	 *  保存一条商品分类
	 * @param goodsCategory
	 * @return
	 */
	@Override
	public JSONObject saveOneGoodsCategoryService(GoodsCategory goodsCategory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCategoryDao.save(goodsCategory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCategory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	/**
	 * 更新一条商品分类
	 */
	@Override
	public JSONObject updateOneGoodsCategoryService(GoodsCategory goodsCategory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCategoryDao.update(goodsCategory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCategory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	/**
	 * 删除一条商品分类记录
	 */
	@Override
	public JSONObject deleteOneGoodsCategoryService(GoodsCategory goodsCategory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCategoryDao.delete(goodsCategory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCategory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public GoodsCategory findOneGoodsCategoryService(Map<String, Object> condMap)
	{
		return this.goodsCategoryDao.findOne(condMap);
	}

	@Override
	public List<GoodsCategory> findCondListGoodsCategoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%，实现模糊匹配*/
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页*/
			/* 查询总的记录数*/
			condMap.put("pageMarking", "true");
			List<GoodsCategory> goodsCategoryList =this.goodsCategoryDao.findCondList(condMap);
			/* 取得总记录数*/
			if (goodsCategoryList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(goodsCategoryList.get(0).getId());
			}
			
			/*开始分页,查询数据*/
			condMap.put("pageMarking", "false");
			/*limit ?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.goodsCategoryDao.findCondList(condMap);
		}
		/*不分页查询数据*/
		return this.goodsCategoryDao.findCondList(condMap);
	}

	/**
	 *  查询整棵树
	 *   [
			{ id:1, pId:0, name:"一级分类", open:true},
			{ id:11, pId:1, name:"二级分类",file=''},
			{ id:111, pId:11, name:"三级分类","file":"http://www.baidu.com"},
			{ id:112, pId:11, name:"三级分类"},
			{ id:113, pId:11, name:"三级分类"},
			{ id:114, pId:11, name:"三级分类"},
			{ id:115, pId:11, name:"三级分类"},
			{ id:12, pId:1, name:"二级分类 1-2"},
			{ id:121, pId:12, name:"三级分类 1-2-1"},
			{ id:122, pId:12, name:"三级分类 1-2-2"},
		]
	 */
	@Override
	public JSONArray findTreeGoodsCategoryService(List<GoodsCategory> goodsCategoryList,String rootPath)
	{
		JSONArray resultArr = new JSONArray();
		/*循环这个数组*/
		for (Iterator iterator = goodsCategoryList.iterator(); iterator.hasNext();)
		{
			GoodsCategory goodsCate = (GoodsCategory) iterator.next();
			/*拼装数据*/
			this.findTreeGoodsCategoryServiceUtil(resultArr, goodsCate,rootPath,0);
		}
		return resultArr;
	}

	/**
	 *  查询树形分类的工具类
	 * @param resultArr  存放整棵数的容器
	 * @param goodsCategory  商品的分类
	 * @param rootPath  叶子节点点击跳转的路径
	 * @param level  分类的层级
	 */
	private void findTreeGoodsCategoryServiceUtil(JSONArray resultArr,GoodsCategory goodsCategory,String rootPath,int level)
	{
		if (goodsCategory != null)
		{
			/*主要用于字符串的拼装,在拼装字符串时不需要重新创建一个新的对象*/
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < level; i++)
			{
				sb.append("|-->");
			}
			/*拼装出来的格式:|-->|-->|-->国产品牌*/
			/*
			 * 对于为什么需要重新取一个新的名字,是因为不能在Name上直接该,因为Name属性在后面还有用到
			 * */
			goodsCategory.setTreeName(sb.toString()+goodsCategory.getName());
			
			JSONObject goodsCategoryJSON = new JSONObject();
			goodsCategoryJSON.put("id", goodsCategory.getId()+"");
			goodsCategoryJSON.put("pId", goodsCategory.getParentId()+"");
			goodsCategoryJSON.put("name", goodsCategory.getName()+"");
			goodsCategoryJSON.put("treeName",goodsCategory.getTreeName() +"");
			/*设置只有是根节点时才展开,这个是根据自己的喜好来设置*/
			if (goodsCategory.getParentId() == 0)
			{
				goodsCategoryJSON.put("open", "true");
				
			}
			/*判断是否是叶子节点,只有是没有了子节点才能有链接*/
			if (goodsCategory.getChildCategoryList().size() == 0)
			{
				goodsCategoryJSON.put("file", rootPath +"/back/goods/categoryUpdate.htm?cid="+goodsCategory.getId());
				/*做标记,能走到这里面,标记是叶子节点*/
				goodsCategoryJSON.put("isLeaf", "1");
			}else
			{
				/*走这里面说明不是叶子节点,以0作为标记*/
				goodsCategoryJSON.put("isLeaf", "0");
			}
			resultArr.add(goodsCategoryJSON);
			/*查询子节点*/
			if (goodsCategory.getChildCategoryList().size() >0)
			{
				for (Iterator iterator = goodsCategory.getChildCategoryList().iterator(); iterator.hasNext();)
				{
					GoodsCategory goodsCategoryChild = (GoodsCategory) iterator.next();
					this.findTreeGoodsCategoryServiceUtil(resultArr, goodsCategoryChild,rootPath,level +1);
				}
			}
		}
	}
	
	/**********************************商品分类模块结束******************************/
	
	/**********************************商品模块开始******************************/
	/**
	 *  保存一条商品
	 * @param goods
	 * @return
	 */
	@Override
	public JSONObject saveOneGoodsService(Goods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.save(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject saveOneGoodsService(Goods goods,Map<String, Object> paramsMap)
	{
		/*先保存其它的字段,成功后进入图片保存阶段*/
		JSONObject resultJSON = this.saveOneGoodsService(goods);
		if ("0".equalsIgnoreCase(resultJSON.getString("code")))
		{
			/* 保存品牌成功 */
			/*
			 * 上传图片:
			路径
			网络路径:http://127.0.0.1:8080/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			真实路径:d:/project/shop_back/uploadfile/brand/2017-02-13/uuid.jpg
			相对路径:/uploadfile/brand/2017-02-13/uuid.jpg
			 * */
			/*获取原始文件的名称*/
			String oriFileName = paramsMap.get("oriFileName") + "";
			/*获取输入流*/
			InputStream inputStream = (InputStream) paramsMap.get("inputStream");
			
			if (paramsMap.get("oriFileName") != null && !"".equalsIgnoreCase(oriFileName))
			{
				/*相对路径*/
				String relaPath = ConstatFinalUtil.BUNDLE.getString("file.relaPath");
				relaPath  += "goods/"+ this.dateFormatUtil.formatDate(new Date()) +	"/"
				+UUID.randomUUID().toString().replaceAll("-", "")
				+ oriFileName.substring(oriFileName.lastIndexOf("."), oriFileName.length());
				
				/*拼装真实路径*/
				String truePath = ConstatFinalUtil.BUNDLE.getString("file.truePath") +relaPath;
				File tarFile = new File(truePath);
				
				try
				{
					/*判断父目录是否存在*/
					if (!tarFile.getParentFile().exists())
					{
						/*不存在就创建*/
						tarFile.getParentFile().mkdirs();
					}
					/*文件输出流*/
					FileOutputStream fos = new FileOutputStream(tarFile);
					/*文件拷贝*/
					if (!this.fileUtil.copyFile(inputStream, fos))
					{
						ConstatFinalUtil.LOGGER.error("拷贝文件失败了," ,tarFile);
						/* 拷贝文件失败,抛异常 */
						String str = null ; 
						str.toString() ; 
					}else
					{
						/*拷贝成功,将相对路径储存到数据库中*/
						goods.setListImgPath(relaPath);
						/*进行更新,将图片更新上去*/
						this.updateOneGoodsService(goods);
					}
				} catch (FileNotFoundException e)
				{
					e.printStackTrace();
				}
			}
		}
		return resultJSON;
	}
	
	
	/**
	 * 更新一条商品
	 */
	@Override
	public JSONObject updateOneGoodsService(Goods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.update(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsService(Goods goods,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.updateOneGoodsService(goods);
		if ("0".equalsIgnoreCase(resultJSON.getString("code")))
		{
			String oriFileName = paramsMap.get("oriFileName") + "";
			InputStream inputStream = (InputStream) paramsMap.get("inputStream");
			/*如果不加以判断,一旦oriFileName为null或者为空字符串,下面拼装路径将会报错,所有必须先加个判断*/
			if (paramsMap.get("oriFileName") != null && !"".equalsIgnoreCase(oriFileName) )
			{
				/*说明其它信息更新成功,现在处理图片*/
				/*老规矩,拼装路径 ,相对路径*/
				String relaPath = ConstatFinalUtil.BUNDLE.getString("file.relaPath");
				relaPath += "/goods/" + dateFormatUtil.formatDate(new Date()) + "/" + UUID.randomUUID()
				.toString().replaceAll("-", "") + 
				oriFileName.substring(oriFileName.lastIndexOf("."), oriFileName.length());
				
				/*真实路径*/
				String truePath = ConstatFinalUtil.BUNDLE.getString("file.truePath") + relaPath;
				File tarFile = new File(truePath);
				
				/*获取老文件*/
				String oldFilePath = goods.getListImgPath();
				File oldFile = new File(ConstatFinalUtil.BUNDLE.getString("file.truePath") + oldFilePath);
				try
				{
					/*判断父目录是否存在*/
					if (!tarFile.getParentFile().exists())
					{
						/*不存在就创建*/
						tarFile.getParentFile().mkdirs();
					}
					/*存在,就直接使用文件输出流*/
					FileOutputStream fos = new FileOutputStream(tarFile);
					if (!this.fileUtil.copyFile(inputStream, fos))
					{
						ConstatFinalUtil.LOGGER.error("拷贝文件失败了{}",tarFile);
					}else
					{
						goods.setListImgPath(relaPath);
						JSONObject resultGoodsJSON = this.updateOneGoodsService(goods);
						if ("0".equalsIgnoreCase(resultGoodsJSON.getString("code")))
						{
							/*证明更新图片成功,这个时候就需要删除老文件*/
							if (oldFile.exists())
							{
								if (!oldFile.delete())
								{
									ConstatFinalUtil.LOGGER.error("删除老文件失败了{}",oldFile);
								}
							}
						}
					}
				} catch (FileNotFoundException e)
				{
				}
			}
		}
		return resultJSON;
	}
	/**
	 * 删除一条商品记录
	 */
	@Override
	public JSONObject deleteOneGoodsService(Goods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.delete(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public Goods findOneGoodsService(Map<String, Object> condMap)
	{
		return this.goodsDao.findOne(condMap);
	}

	@Override
	public List<Goods> findCondListGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%，实现模糊匹配*/
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页*/
			/* 查询总的记录数*/
			condMap.put("pageMarking", "true");
			List<Goods> goodsList =this.goodsDao.findCondList(condMap);
			/* 取得总记录数*/
			if (goodsList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(goodsList.get(0).getId());
			}
			
			/*开始分页,查询数据*/
			condMap.put("pageMarking", "false");
			/*limit ?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.goodsDao.findCondList(condMap);
		}
		/*不分页查询数据*/
		return this.goodsDao.findCondList(condMap);
	}
	
	
	/**********************************商品模块结束******************************/
}
